<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
class Notify extends Api{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
//    public $file=ROOT_PATH."log.txt";
    public function index(){
        $raw_xml=file_get_contents("php://input");
        libxml_disable_entity_loader(true);
        $ret=simplexml_load_string($raw_xml,'SimpleXMLElement',LIBXML_NOCDATA);
        $reten=json_encode($ret);
        $raw=json_decode($reten,true);
        if($raw['return_code']=='SUCCESS'  && $raw['result_code']=='SUCCESS'){
            $orser_no = $raw['out_trade_no'];
            $content = "时间： ".date("Y-m-d H:i:s",time())."\r\n"."单号：".$orser_no;
            file_put_contents($this->file, $content,FILE_APPEND);
            $res = Db::name('order')->where('order_no',$orser_no)->find();
            $contents = "查询： ".json_encode($res)."\r\n"."单号：".$orser_no;
            file_put_contents($this->file, $contents,FILE_APPEND);
            if($res['status'] == 1){
                // 更改订单状态
                Db::name('order')->where('order_no',$orser_no)->update(['status'=>2]);
//                Db::name('spike')->where('id',$res['goods_id'])->setInc('sum',$res['num']);
//                Db::name('spike')->where('id',$res['goods_id'])->setDec('num',$res['num']);
            }
            echo 'SUCCESS';
        }else{
            $content = "时间： ".date("Y-m-d H:i:s",time())."\r\n"."失败：";
            file_put_contents($this->file, $content,FILE_APPEND);
            echo "FAIL";
        }
    }
}